﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ProjectShop
{
    public partial class FrmShopList : Form
    {
        ProjectCommon.ClassCommon cc;
        DataTable dtShopList;

        public FrmShopList(ProjectCommon.ClassCommon aClassCommon)
        {
            InitializeComponent();
            cc = aClassCommon;
            btnShowAll.PerformClick();
        }

        private void GetShopTable(string aWhere)
        {
            string sql = string.Empty;
            string sPowerRight = cc.dtLoginUser.Rows[0]["PowerRight"].ToString();
            if (sPowerRight[1] == '0')
            {
                sql = @"select * from View_ShopList0 where " + aWhere;
            }
            else
            {
                sql = @"select * from View_ShopList1 where " + aWhere;
            }
            SqlDataAdapter sda = new SqlDataAdapter(sql, cc.scKengee_IT_MIS);
            dtShopList = new DataTable();
            cc.scKengee_IT_MIS.Open();
            sda.Fill(dtShopList);
            cc.scKengee_IT_MIS.Close();
            dgvShopList.DataSource = dtShopList;
            sda.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmShopInfo fsi = new FrmShopInfo(cc, ProjectCommon.ClassCommon.OperateState.osNew, "");
            fsi.ShowDialog();
            fsi.Dispose();
            GetShopTable("1=1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvShopList.CurrentRow != null)
            {
                string sFastCode = dgvShopList.CurrentRow.Cells["快速代码"].Value.ToString();
                FrmShopInfo fsi = new FrmShopInfo(cc, ProjectCommon.ClassCommon.OperateState.osModify, sFastCode);
                fsi.ShowDialog();
                fsi.Dispose();
                GetShopTable("1=1");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (tbFindKey.Text.Trim().Length > 0)
            {
                for (int i = 0; i < dgvShopList.RowCount; i++)
                {
                    for (int j = 0; j < dgvShopList.ColumnCount; j++)
                    {
                        string sContect = dgvShopList.Rows[i].Cells[j].Value.ToString();
                        if (sContect.Contains(tbFindKey.Text))
                        {
                            dgvShopList.CurrentCell = dgvShopList.Rows[i].Cells[j];
                        }
                    }
                }
            }
        }

        private void tbFindKey_KeyPress(object sender, KeyPressEventArgs e)
        {
            btnFind.PerformClick();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            GetShopTable("1=1");
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            if (dgvShopList.CurrentRow != null)
            {
                string sFilter = dgvShopList.Columns[dgvShopList.CurrentCell.ColumnIndex].Name + " like '%" + tbFindKey.Text + "%'";
                DataTable dtSelect = ProjectCommon.ClassCommon.GetSelectTable(dtShopList, sFilter);
                dgvShopList.DataSource = dtSelect;
            }
        }

        private void FrmShopList_Shown(object sender, EventArgs e)
        {
            btnShowAll.PerformClick();
        }

        private void dgvShopList_DataSourceChanged(object sender, EventArgs e)
        {

        }

        private void dgvShopList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            for (int i = 0; i < dgvShopList.RowCount; i++)
            {
                string cbCircuitTypes = dgvShopList.Rows[i].Cells["线路类型"].Value.ToString();
                if (cbCircuitTypes.Contains("宽带"))
                {
                    dgvShopList.Rows[i].DefaultCellStyle.BackColor = SystemColors.GradientInactiveCaption;
                }
            }
        }
    }
}
